package com.xiaoma.mframe.project.tool.gen.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaoma.mframe.general.utils.AjaxResult;
import com.xiaoma.mframe.general.web.controller.BaseController;
import com.xiaoma.mframe.general.web.page.TableDataInfo;
import com.xiaoma.mframe.project.tool.gen.entity.User;
import com.xiaoma.mframe.project.tool.gen.service.IUserService;

/**
 * 用户 信息操作处理
 * 
 * @author machuxin
 * @date 2018-10-26
 */
@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
	private String prefix = "system/user";

	@Autowired
	private IUserService userService;

	@GetMapping()
	public String user() {
		return prefix + "/user";
	}

	/**
	 * 查询用户列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(User user) {
		startPage();
		List<User> list = userService.selectUserList(user);
		return getDataTable(list);
	}

	/**
	 * 新增用户
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存用户
	 */
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(User user) {
		return toAjax(userService.insertUser(user));
	}

	/**
	 * 修改用户
	 */
	@GetMapping("/edit/{userId}")
	public String edit(@PathVariable("userId") Integer userId, ModelMap mmap) {
		User user = userService.selectUserById(userId);
		mmap.put("user", user);
		return prefix + "/edit";
	}

	/**
	 * 修改保存用户
	 */
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(User user) {
		return toAjax(userService.updateUser(user));
	}

	/**
	 * 删除用户
	 */
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(userService.deleteUserByIds(ids));
	}

}
